﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Reborn_WorldServer.App1AppHandler
{
    public class RandomGenerator
    {
        private object lockObject = new object();
        private int currentSeconds;
        private int currentMilliseconds;
        private Random random;
        public RandomGenerator()
        {
            DateTime now = DateTime.Now;
            currentSeconds = now.Second;
            currentMilliseconds = now.Millisecond;

            random = new Random(getSeed());
        }
        private int getSeed()
        {
            return currentSeconds * currentMilliseconds;
        }

        public int Next(int high)
        {
            return Next(0, high);
        }

        private int interlockedSeedIncrement = 0;
        public int Next(int low, int high)
        {
            Interlocked.Increment(ref interlockedSeedIncrement);

            if (interlockedSeedIncrement > 10000)
                interlockedSeedIncrement = 0;

            Random random = new Random(DateTime.Now.Millisecond * interlockedSeedIncrement);
            return random.Next(low, high);

            //lock (lockObject)
            //{
            //    DateTime now = DateTime.Now;

            //    if (now.Second != currentSeconds || now.Millisecond != currentMilliseconds)
            //    {
            //        currentSeconds = now.Second;
            //        currentMilliseconds = now.Millisecond;

            //        random = new Random(getSeed());
                    
            //    }

            //    return random.Next(low, high);
            //}
        }
    }
}
